/**
 * @description:
 * @Author yyp
 * @time 2022/3/30
 */
public class DeadBlock {
    public static void main(String[] args) {
        DeadBlockDemo d1 = new DeadBlockDemo(true);
        DeadBlockDemo d2 = new DeadBlockDemo(false);
        d1.start();
        d2.start();
    }
}

class DeadBlockDemo extends Thread{
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;

    public DeadBlockDemo(boolean flag){
        this.flag = flag;
    }

    @Override
    public void run(){
        if(flag){
            synchronized (o1){
                System.out.println(Thread.currentThread().getName()+"进入1");
                try {
                    wait(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2){
                    System.out.println(Thread.currentThread().getName()+"进入2");
                }
            }
        }else {
            synchronized (o2){
                System.out.println(Thread.currentThread().getName()+"进入2");
                synchronized (o1){
                    System.out.println(Thread.currentThread().getName()+"进入1");
                }
            }
        }
    }
}
